using UnityEngine;
using System.Collections;

public class MUIManager : MonoBehaviour
{
    public CustomScene scene;
	public MText t;
	
	bool b = true;

    static MUIManager ms_pkThis = null;
	MScrollView scrollView = null;

    void Start()
    {
        ms_pkThis = this;
    }

    public static MUIManager Instance()
    {
        return ms_pkThis;
    }
	
	void Update()
	{
		RaycastHit hit;
		Ray ray;
		/*
		if( scrollView != null )
		{
			if( Input.touchCount > 0 && scrollView.IsMove() )
			{
				scrollView.gameObject.SendMessage("OnEvent", Input.touches[0], SendMessageOptions.DontRequireReceiver);
				return;
			}
			else 
			{
				scrollView = null;
			}			
		}
		*/
		
		if( Application.platform == RuntimePlatform.Android ||
			Application.platform == RuntimePlatform.IPhonePlayer )
		{
			if( Input.touchCount > 0 )
			{
				ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
				if( Physics.Raycast(ray, out hit, Mathf.Infinity) )
				{
					hit.transform.gameObject.SendMessage("OnEvent", Input.touches[0], SendMessageOptions.DontRequireReceiver);
				}
			}
		}
		else
		{
			if( Input.GetButtonDown("Fire1") )
			{
				ray = Camera.main.ScreenPointToRay(Input.mousePosition);
				
				if( Physics.Raycast (ray,out hit, Mathf.Infinity) )
				{
					hit.transform.gameObject.SendMessage("OnEvent", t, SendMessageOptions.DontRequireReceiver);
				}
			}
		}
		/*
		for(int i=0; i<Input.touchCount; ++i)
		{
			ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
			if( Physics.Raycast(ray, out hit, Mathf.Infinity) )
			{
				hit.transform.gameObject.SendMessage("OnEvent", Input.touches[i], SendMessageOptions.DontRequireReceiver);
			}
		}
		*/
		
	}
	
	void OnMouseUp()
	{
	}

    public void Event(int number, object obj)
    {
        scene.OnUIEvent(number, obj);
    }
	
	public void SetScrollView(MScrollView sv)
	{
		scrollView = sv;
	}
}

